package m_channellist

import (
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"time"
	"wx-api/adx-bid/common"
	"wx-api/adx-bid/config"
)

type ChannelList struct {
	Id             bson.ObjectId `bson:"_id,omitempty" json:"id,omitempty"`
	Uqid           string        `bson:"-" json:"uqid,omitempty"`                        //channelid
	Code           string        `bson:"code" json:"code,omitempty"`                     //channelid
	Name           string        `bson:"name" json:"name,omitempty"`                     //名称
	BidUrl         string        `bson:"bid_url" json:"bid_url,omitempty"`               //DSP_URL
	Identification string        `bson:"identification" json:"identification,omitempty"` //标示
	ApiType        string        `bson:"api_type" json:"api_type,omitempty"`             //协议类型
	PvHour         int           `bson:"pvhour" json:"pvhour,omitempty"`                 //曝光有效期：默认2小时
	Rate           int           `bson:"rate" json:"rate,omitempty"`                     //返点比例:整数
	Dspid          string        `bson:"dspid" json:"dspid,omitempty"`                   //DSPID
	Token          string        `bson:"token" json:"token,omitempty"`                   //TOKEN
	Itoken         string        `bson:"itoken" json:"itoken,omitempty"`                 //ITOKEN
	Flag           int           `bson:"flag" json:"flag,omitempty"`                     //1 开启 2 关闭
	Status         int           `bson:"status" json:"status,omitempty"`                 //1 正常 2 删除
	CreateTime     int64         `bson:"create_time" json:"create_time,omitempty"`       //创建时间
	UpdateTime     int64         `bson:"update_time" json:"update_time,omitempty"`       //更新时间
	Version        string        `bson:"version" json:"version,omitempty"`               //版本号
}

func dbGet() *common.TkMongo {
	return config.MgTableChannelList
}

func Save(a ChannelList) (string, error) {
	a.Id = bson.NewObjectId()
	tmptime := time.Now().Unix()
	a.CreateTime = tmptime
	a.UpdateTime = tmptime
	a.Flag = 1
	a.Status = 1
	a.Version = config.SysVersion
	err := dbGet().Insert(a)
	return a.Id.Hex(), err
}

// 更新
func Update(a ChannelList) error {
	m := bson.M{
		"name":           a.Name,
		"bid_url":        a.BidUrl,
		"identification": a.Identification,
		"dspid":          a.Dspid,
		"token":          a.Token,
		"itoken":         a.Itoken,
		"api_type":       a.ApiType,
		"pvhour":         a.PvHour,
		"rate":           a.Rate,
		"update_time":    time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": a.Id}, m)
}

// 更新状态
func UpdateFalg(id string, flag int) error {
	m := bson.M{
		"flag":        flag,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": bson.ObjectIdHex(id)}, m)
}

// 删除
func UpdateDel(id string, status int) error {
	m := bson.M{
		"status":      status,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": bson.ObjectIdHex(id)}, m)
}

// 获取所有
func GetAll() []ChannelList {
	list := []ChannelList{}
	_ = dbGet().All(nil, &list)
	return list
}

func GetWAll(a int64) []ChannelList {
	list := []ChannelList{}
	_ = dbGet().All(bson.M{"update_time": bson.M{"$gte": a}}, &list)
	return list
}

// 获取详情
func GetByCode(id string) (*ChannelList, error) {
	a := &ChannelList{}
	err := dbGet().FindOne(bson.M{"_id": bson.ObjectIdHex(id)}, a)
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return a, nil
}
func GetByCk(id, dsp_id string) (*ChannelList, error) {
	a := &ChannelList{}
	err := dbGet().FindOne(bson.M{"_id": bson.ObjectIdHex(id)}, a)
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return a, nil
}

// 列表，带分页
func List(id, name, dsp_id string, flag, status, pageNum, pageSize int) (*common.PageInfo, error) {
	s := bson.M{}
	if id != "" {
		s["_id"] = bson.ObjectIdHex(id)
	}
	if name != "" {
		s["name"] = bson.M{"$regex": bson.RegEx{name, "i"}}
	}
	if dsp_id != "" {
		s["dsp_id"] = dsp_id
	}
	if flag > 0 {
		s["flag"] = flag
	}
	if status > 0 {
		s["status"] = status
	}
	var list []ChannelList
	return dbGet().FindPageInfo(s, "", pageSize, pageNum, "-update_time", &list)
}
